def read_apis():
    api_list = {}
    file = open(file='./data/api_nodes_estimator.csv', mode='r', encoding='utf8')
    lines = file.readlines()
    lines = lines[1:]  # 第一行不用
    for line in lines:
        data = line.split('\t')
        name = data[2][:len(data[2]) - 18]  # 名称, xxx API MASTER RECORD -> xxx
        api_type = ''
        # 筛选 API 类型
        if len(name) > 5 and name[len(name) - 4:] == 'REST':
            api_type = 'REST'
            name = name[:len(name) - 5]
        elif len(name) > 5 and name[len(name) - 4:] == 'JRPC':
            api_type = 'JSON-RPC'
            name = name[:len(name) - 5]
        elif len(name) > 5 and name[len(name) - 4:] == 'gRPC':
            api_type = 'gRPC'
            name = name[:len(name) - 5]
        elif len(name) > 8 and name[len(name) - 7:] == 'XML-RPC':
            api_type = 'XML-RPC'
            name = name[:len(name) - 8]
        elif len(name) > 9 and name[len(name) - 8:] == 'JSON-RPC':
            api_type = 'JSON-RPC'
            name = name[:len(name) - 9]
        elif len(name) > 4 and name[len(name) - 3:] == 'RPC':
            api_type = 'RPC'
            name = name[:len(name) - 4]
        else:
            api_type = 'NORMAL'
        api_list[data[1]] = [name, api_type, data[6]]  # {地址: [名称, API 类型, 类别]}
    file.close()
    return api_list


def read_mashups():
    mash_list = {}
    file = open(file='./data/mashup_nodes_estimator.csv', mode='r', encoding='utf8')
    lines = file.readlines()
    lines = lines[1:]  # 第一行不用
    for line in lines:
        data = line.split('\t')
        if data[1] == 'Undefined':  # 失效 Mashup 去之不用
            continue
        name = data[2][8:]  # 名称, Mashup: xxx -> xxx
        mash_list[name] = data[6]  # {名称: 类别}
    file.close()
    return mash_list


def read_relations():
    rel_list = {}
    file = open(file='./data/m-a_edges.csv', mode='r', encoding='utf8')
    lines = file.readlines()
    lines = lines[1:]  # 第一行不用
    for line in lines:
        data = line.split('\t')
        name = data[0][8:]  # 名称, Mashup: xxx -> xxx
        addr = data[1][:len(data[1]) - 1]  # 去除 \n
        # 可能有多条记录包含相同 Mashup 名称
        if name in rel_list.keys():
            rel_list[name].append(addr)
        else:
            rel_list[name] = [addr]
    file.close()
    return rel_list
